<template>
  <div class="app-container">
    <!-- 头部操作区域 -->
    <div class="header-section">
      <el-page-header @back="goBack" :content="`${dictTitle} - 字典数据管理`" />
      <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" style="margin-left: 20px;">
        添加数据
      </el-button>
    </div>

    <!-- 表格区域 -->
    <el-table v-loading="listLoading" :data="dataList" stripe style="width:100%; margin-top: 20px;">
      <el-table-column type="index" width="60" label="序号" />
      <el-table-column prop="title" label="数据名称" width="200" />
      <el-table-column prop="value" label="数据值" width="200" />
      <el-table-column prop="sortOrder" label="排序" width="100" />
      <el-table-column label="状态" width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 0 ? 'success' : 'danger'">
            {{ scope.row.status === 0 ? '启用' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="备注" min-width="200" />
      <el-table-column label="操作" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button type="success" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区 -->
    <div class="paging_device">
      <el-pagination
        :current-page="pageNumber"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalElements"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 添加/编辑字典数据弹窗 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" :close-on-click-modal="false" width="30%">
      <el-form ref="dataForm" :model="dataForm" :rules="dataRules" label-width="100px">
        <el-form-item label="数据名称" prop="title">
          <el-input v-model="dataForm.title" placeholder="请输入数据名称" />
        </el-form-item>
        <el-form-item label="数据值" prop="value">
          <el-input v-model="dataForm.value" placeholder="请输入数据值" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="dataForm.status">
            <el-radio :label="0">启用</el-radio>
            <el-radio :label="-1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="sortOrder">
          <el-input-number v-model="dataForm.sortOrder" :min="0" />
        </el-form-item>
        <el-form-item label="备注" prop="description">
          <el-input v-model="dataForm.description" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getDictDataByCondition, addDictData, editDictData, deleteDictData } from '@/api/dictData'

export default {
  name: 'DictDataManage',
  data() {
    return {
      dictId: '',
      dictType: '',
      dictTitle: '',
      listLoading: false,
      dataList: [],
      pageNumber: 1,
      pageSize: 10,
      totalElements: 0,
      dialogVisible: false,
      dialogTitle: '添加数据',
      isEdit: false,
      dataForm: {
        id: '',
        title: '',
        value: '',
        status: 0,
        sortOrder: 0,
        description: '',
        dictId: ''
      },
      dataRules: {
        title: [
          { required: true, message: '请输入数据名称', trigger: 'blur' }
        ],
        value: [
          { required: true, message: '请输入数据值', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.dictId = this.$route.query.dictId
    this.dictType = this.$route.query.dictType
    this.dictTitle = this.$route.query.dictTitle
    if (!this.dictId) {
      this.$message.error('缺少字典ID参数')
      this.goBack()
      return
    }
    this.getDataList()
  },
  methods: {
    // 返回
    goBack() {
      this.$router.back()
    },
    // 获取字典数据列表
    getDataList() {
      this.listLoading = true
      const params = {
        dictId: this.dictId,
        pageNumber: this.pageNumber - 1, // 后端从0开始
        pageSize: this.pageSize
      }
      getDictDataByCondition(params).then(res => {
        if (res.code === 200) {
          this.dataList = res.result.content
          this.totalElements = res.result.totalElements
        }
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },
    // 分页大小变化
    handleSizeChange(val) {
      this.pageSize = val
      this.pageNumber = 1
      this.getDataList()
    },
    // 当前页变化
    handleCurrentChange(val) {
      this.pageNumber = val
      this.getDataList()
    },
    // 添加数据
    handleAdd() {
      this.dialogTitle = '添加数据'
      this.isEdit = false
      this.dataForm = {
        title: '',
        value: '',
        status: 0,
        sortOrder: 0,
        description: '',
        dictId: this.dictId
      }
      this.dialogVisible = true
    },
    // 编辑数据
    handleEdit(row) {
      this.dialogTitle = '编辑数据'
      this.isEdit = true
      this.dataForm = {
        id: row.id,
        title: row.title,
        value: row.value,
        status: row.status,
        sortOrder: row.sortOrder,
        description: row.description,
        dictId: row.dictId
      }
      this.dialogVisible = true
    },
    // 提交
    handleSubmit() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          if (this.isEdit) {
            editDictData(this.dataForm).then(res => {
              if (res.code === 200) {
                this.$message.success('编辑成功')
                this.dialogVisible = false
                this.getDataList()
              }
            })
          } else {
            addDictData(this.dataForm).then(res => {
              if (res.code === 200) {
                this.$message.success('添加成功')
                this.dialogVisible = false
                this.getDataList()
              }
            })
          }
        }
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('确定要删除该数据吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDictData(row.id).then(res => {
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.getDataList()
          }
        })
      })
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 20px;
}

.header-section {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.paging_device {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
